﻿<%@ WebHandler Language="C#" Class="Org" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Org : IHttpHandler,IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public string ErrMsg { get; set; }
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["departname"], context.Request["parentid"],
                        context.Request["sortnum"], context.Request["remark"],
                        context.Request["groupmaster"]))
                    {
                        context.Response.Write(success("", "OrgList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "OrgList"));
                    }
                    else
                    {
                        context.Response.Write(failure(ErrMsg));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["departid"], context.Request["departname"], context.Request["parentid"],
                        context.Request["sortnum"], context.Request["remark"],
                        context.Request["groupmaster"]))
                    {
                        context.Response.Write(success("", "OrgList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
            }
        }
    }

    public bool Del(string id)
    {
        var departManager = appContext.GetObject("S_DepartManager") as GONES.IBLL.IS_DepartManager;
        try
        {
            if (departManager.GetOrgCountByParentId(id) > 0)
            {
                ErrMsg = "该部门下存在子部门不能删除！";
                return false;
            }
            if (departManager.GetEmpCountByOrgId(id) > 0)
            {
                ErrMsg = "该部门下存在雇员不能删除！";
                return false;
            }
            departManager.Delete(id);
            return true;
        }
        catch { return false; }
    }


    
    private GONES.Model.S_Depart GetOrgModel(string id, string departname, string parentid, string sortnum, string remark, string groupmaster)
    {
        var departManager = appContext.GetObject("S_DepartManager") as GONES.IBLL.IS_DepartManager;
        GONES.Model.S_Depart depart = new GONES.Model.S_Depart();
        if (id.Trim() != "")
            depart = departManager.Get(id.Trim());
        depart.ID = id.Trim();
        depart.DepartName = departname.Trim();
        depart.ParentId = parentid.Trim();
        depart.SortNum =int.Parse(sortnum.Trim());
        depart.Remark = remark.Trim();
        depart.GroupMaster = groupmaster.Trim();
        return depart;
    }
    public bool Add(string departname, string parentid, string sortnum, string remark,string groupmaster)
    {
        var depInfoManager = appContext.GetObject("S_DepartManager") as GONES.IBLL.IS_DepartManager;
        GONES.Model.S_Depart depart = new GONES.Model.S_Depart();
        try
        {
            string id = "";
            depart = GetOrgModel(id, departname, parentid, sortnum, remark, groupmaster);
            depart.ID = GONES.Tools.Common.Rnd.Guid.ToString().Trim();
            depart.CreateUser = loginInfo.UserName.Trim();
            depInfoManager.Add(depart);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string id, string departname, string parentid, string sortnum, string remark, string groupmaster)
    {
        var depInfoManager = appContext.GetObject("S_DepartManager") as GONES.IBLL.IS_DepartManager;
        GONES.Model.S_Depart depart = new GONES.Model.S_Depart();
        try
        {
            depart = GetOrgModel(id, departname, parentid, sortnum, remark, groupmaster);
            depart.ModifyUser = loginInfo.UserName;
            depInfoManager.Edit(depart);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}